package com.jinzhi.jzweb.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

public class FilesSizeUtil {

    public static boolean fileSize(String url){
        Map<Object,Object> map = new HashMap<>();
        File file = new File(url);
        FileChannel fc = null;
        if(file.exists() && file.isFile()){
            try {
                FileInputStream fs = new FileInputStream(file);
                fc = fs.getChannel();
                map.put("fileSize",fc.size());
                System.out.println(fc.size() + "-----fc.size()");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        map.put("fileByte",file.length());
        System.out.println(file.length() + "-----file.length  B");
        System.out.println(file.length() * 1024 + "-----file.length  kb");
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int width = bi.getWidth();
        int height = bi.getHeight();
        map.put("fileWidth",bi.getWidth());
        map.put("fileHeight",bi.getHeight());
        System.out.println("宽：像素-----" + width + "高：像素"  + height);

        return false;
    }

    public static boolean fileSize1(String url){
        try {
            // 生成一个BufferedImage对象
            BufferedImage bi = ImageIO.read(new File(url));
            // 取得图片高度
            int i = bi.getHeight();
            // 取得图片宽度
            int j = bi.getWidth();
            System.out.println("图片高度为： " + i + "," + "图片宽度为： " + j);
            System.out.println("图片尺寸为：" + j + " x " + i);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean imgJudge(String url){
        boolean b = true;
        File file = new File(url);
        FileChannel fc = null;
        if(file.exists() && file.isFile()){
            try {
                FileInputStream fs = new FileInputStream(file);
                fc = fs.getChannel();
                //图片不能大于一兆
                if(fc.size() > 1048576){
                    b =false;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int width = bi.getWidth();
        int height = bi.getHeight();
        if(width > 295 || height > 413){
            if(b == true){
                b =false;
            }
        }
        return b;
    }


    public static void main(String[] args) {

        boolean b = imgJudge("D:\\Workspaces\\code\\x\\upload\\20200313\\2\\b\\wx65438d98141bfa19.o6zAJs96XvgFfe7jaZrS1JyTKy20.xhnjNrx2c8xk5b163994dbffaecbf982492b5fd87bad.jpg");
        System.out.println("map:::::" + b);

    }
}
